package com.holyant.goods.utils;

import com.holyant.goods.dto.GdsCategoryDto;

import java.util.*;

public class CategoryUtils {

    /**
     * 获取顶级分类下所有子分类
     * @param categoryList
     * @return
     */
    public static Map<Long, List<Long>> getCategoryChildList(List<GdsCategoryDto> categoryList) {
        Map<Long, List<Long>> categoryMap = new HashMap<>();
        for (GdsCategoryDto category : categoryList) {
            if (category.getParentId() == 0) {
                categoryMap.put(category.getId(), getChildrenIdList(category, categoryList));
            }
        }
        return categoryMap;
    }

    /**
     * 递归获取子分类
     * @param category
     * @param categoryList
     * @return
     */
    public static List<Long> getChildrenIdList(GdsCategoryDto category, List<GdsCategoryDto> categoryList) {
        List<Long> children = new ArrayList<>();
        for (GdsCategoryDto child : categoryList) {
            if (Objects.equals(child.getParentId(), category.getId())) {
                children.add(child.getId());
                children.addAll(getChildrenIdList(child, categoryList));
            }
        }
        return children;
    }

}